package com.shop.shoporder.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.shop.shoporder.entity.PostJson;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class InterfaceVer {

    /**
     * 接口验证
     * @param request
     * @param jsonParam
     * @return
     */
    public static Map<String, Object> Security(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = new HashMap<>();
        //验证IP
        String IP = UserRequest.getIp(request);
        System.out.println(IP);
        //if (Config.ipz.contains(";"+ IP +";")) {
        if (1==1) {
            // 直接将json信息打印出来
            System.out.println("JSON："+jsonParam.toJSONString());

            //将接收到的数据转化为对象
            PostJson postJson = JSON.parseObject(jsonParam.toJSONString(), new TypeReference<PostJson>() {
            });
            //验证密钥
            String modKey = postJson.getModKey();
            String key = Encrypt.decrypt(modKey);
            if (key.equals(Config.key)) {
                // 直接将json信息打印出来
                System.out.println("密钥:" + key);
                map.put("code","000000");
                map.put("AID",postJson.getAID());
                map.put("UID",postJson.getUID());
                map.put("parm",postJson.getParm());
            }
            else
            {
                //KEY错误
                map.put("code","666011");
            }
        }
        else {
            //IP不被容许
            map.put("code","666012");
        }
        return map;
    }

    public static Integer VerNumber(String value){
        if (value == null) {
            return 0;
        } else if ("".equals(value)) {
            return 0;
        } else if(value.toLowerCase().equals("null")) {
            return 0;
        }else {
            try {
            return Integer.parseInt(value);
            }catch (Exception e){
            return 0;
            }
        }
    }

    public static String VerStr(String value){
        if (value == null) {
            return "";
        } else if ("".equals(value)) {
            return "";
        }else if(value.toLowerCase().equals("null")) {
            return "";
        }else if(value.toLowerCase().equals("undefined")) {
            return "";
        }else {
            return value;
        }
    }
    public static Double VerDoubleNumber(String value){
        if (value == null) {
            return 0.00;
        } else if ("".equals(value)) {
            return 0.00;
        } else {
            try {
                return Double.parseDouble(value);
            }catch (Exception e){
                return 0.00;
            }
        }
    }

    public static double formatDouble1(double d) {
        return (double)Math.round(d*100)/100;
    }

        public static Date addDateMinut(String day, int hour){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(day);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (date == null)
            return null;
        //System.out.println("front:" + format.format(date)); //显示输入的日期
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);// 24小时制
        date = cal.getTime();
        //System.out.println("after:" + format.format(date));  //显示更新后的日期
        cal = null;
        return date;
    }

    public static Date addDateMinutS(String day, int minute){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(day);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (date == null)
            return null;
        //System.out.println("front:" + format.format(date)); //显示输入的日期
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, minute);//
        date = cal.getTime();
        //System.out.println("after:" + format.format(date));  //显示更新后的日期
        cal = null;
        return date;
    }

}
